var api = require('../../utils/api.js');
var app = getApp();
Page({
    data: {
        info:'',
        code:'',
        lista:['入库中', '制作中', '校对中',  '已交图' , '待返回', '已返货'],
        gcode:'',
        id:'',
        paise:'0',
        dtimgs:' ',
        sximgs:' ',
        lists:[]
    },
    printinfo(ret){
        var id=ret.currentTarget.dataset.id;
        this.printvideo(id);
    },

    printvideo(id){
        wx.showLoading({
          title: '提交中...'
        })
        api.post({
            url: 'Caseinfo/printinfobyid',
            data: {id:id},
            success: data => {
                wx.hideLoading();
                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })
            }
        })

    },

    getlogbycode(ret){
        var id=ret.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/pages/about/logs?id='+id
        })
        console.log(id)
    },

    getcodebycode(ret){
        console.log(ret)

        var code=ret.currentTarget.dataset.code;

        wx.showLoading({
          title: '请求中...',
          icon:'none'
        })

        api.get({
            url: 'portal/qrcode/getqrcode',
            data: {'code':code},
            success: data => {
                console.log(data)

                wx.hideLoading();

                var img=data.data;
              
                wx.previewImage({
                    current: img, 
                    urls: [img] 
                });
        
               
            }
        })


    },

    seeimgs(e) {
        var img=this.data.info.img;
        console.log(img);
        wx.previewImage({
            current: img,
            urls: [img] 
        });

    },


    diaopai(){
        var id=this.data.id;
        var dtimgs=this.data.dtimgs;
        var sximgs=this.data.sximgs;
        var that=this;

        api.post({
            url: 'order/setgoodsphone',
            data: {'id':id,dtimgs:dtimgs,sximgs:sximgs},
            success: data => {
                console.log(data)
                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })
                if(data.code==1){
                    that.setData({
                        paise:0
                    })
                    that.getgoodinfo();

                }

               
            }
        })


    },

    currentPageNumber: 1,
    setgooddesc(ret){
        console.log(ret);   
        var value=ret.detail.value;
        var id=this.data.id
        api.post({
            url: 'user/setgooddesc',
            data: {'id':id,'desc':value},
            success: data => {
             wx.showToast({
               title: '操作成功!',
               icon:'none'
             })
            }
        })


    },

    scanCodeEvent: function(){
        var that = this;
        wx.scanCode({
          onlyFromCamera: true,// 只允许从相机扫码
          success(res){
            JSON.stringify(res)
            console.log(res.result)
            
            that.setData({
                code:res.result
            })
            that.getgoodinfo();
     
          }
        })
      },

      scanCodeEvents: function(){
        var that = this;
        wx.scanCode({
          onlyFromCamera: true,// 只允许从相机扫码
          success(res){
            JSON.stringify('----',res)
            console.log(res.result)
            that.setData({
                gcode:res.result
            })
          }
        })
      },
      
    getgoodinfo(ret){
        console.log(ret);

        var that=this;
        var code=this.data.code;

        api.post({
            url: 'order/getgoodbyid',
            data: {'codes':code},
            success: data => {

                console.log(data)

                if(data.code!=1){
                    wx.showToast({
                      title: data.msg,
                      icon:'none'
                    })
                    return false
                }
                if(data.msg=='多个信息'){

                    var list=data.data;
                    var lists=[];
                    list.forEach(function(item){
                        lists.push(item.title);
                    })
                    wx.showActionSheet({
                        itemList: lists,
                        success (res) {
                            var index=res.tapIndex;
                            var id=list[index].id;
                            console.log(id);
                            
                            api.post({
                                url: 'order/getgoodbyinfoid',
                                data: {'id':id},
                                success: data => {

                                    that.setData({
                                        info:data.data,
                                        id:data.data.id,
                                        gcode:data.data.code,
                                        sximgs:data.data.sximgs,
                                        dtimgs:data.data.dtimgs,
                                        gcode:data.code
                                    })

                                }
                            })

                        }
                    })

                    return false;
                }

                that.setData({
                    info:data.data,
                    id:data.data.id,
                    gcode:data.data.code,
                    sximgs:data.data.sximgs,
                    dtimgs:data.data.dtimgs
                })
            }
        })
    },
    onLoad(ret) {
        this.setData({
            code:ret.code
        })
        this.getgoodinfo();

    },
   
    setback(){

        var code=this.data.id;
        var that=this;
        wx.showActionSheet({
            itemList: ['未退', '已退'],
            success (res) {
                console.log(res.tapIndex)

                api.post({
                    url: 'portal/index/setgoodsbackbycode',
                    data: {'id':code,'back':res.tapIndex},
                    success: data => {
                        that.getgoodinfo();
                    }
                })

            },
            fail (res) {
                console.log(res.errMsg)
            }
        })
    },

    setstatus(ret){
        console.log(ret);
        var that=this;
        var id=this.data.id;
        var i=ret.currentTarget.dataset.i;

        wx.showModal({
            title: '提示',
            content: '确认已经完成?',
            success (res) {
            if (res.confirm) {
                api.post({
                    url: 'order/setgoodsbycode',
                    data: {'id':id,'status':i},
                    success: data => {
                        that.getgoodinfo();
                    }
                })
            }
            }
        })

    },

    setstatusb1(ret){
        console.log(ret);
        var that=this;
        var code=this.data.id;
        var i=ret.currentTarget.dataset.i;
        wx.showModal({
            title: '提示',
            content: '确认已经完成?',
            success (res) {
            if (res.confirm) {
                api.post({
                    url: 'order/setgoodsstatusbycodeb',
                    data: {'code':code,'status':i},
                    success: data => {
                        that.getgoodinfo();
                    }
                })
            } else if (res.cancel) {
            console.log('用户点击取消')
            }
            }
        })
    
    },
    setstatusb(ret){
        console.log(ret);
        var that=this;
        var code=this.data.id;
  
        api.post({
            url: 'order/setgoodsstatusbycode',
            data: {'code':code,'status':1},
            success: data => {
                that.getgoodinfo();
            }
        })
    
    },

    settypes(){
        var that=this;
        var code=this.data.id;
        var list=this.data.lista;

        wx.showActionSheet({
            itemList:list,
            success (res) {
                var index=res.tapIndex;
                var title=list[index];
                api.post({
                    url: 'order/setgoodsbycode',
                    data: {'id':code,'status':res.tapIndex},
                    success: data => {

                        wx.showToast({
                          title: data.msg,
                          icon:'none'
                        })

                        that.getgoodinfo();
                    }
                })
            },
            fail (res) {
              console.log(res.errMsg)
            }
          })
    },

    gettongkuan(){
        var that=this;
        var code=this.data.code;
        var list=this.data.info.tongkuan;
        var tktit=this.data.info.tktit;


        wx.showActionSheet({
            itemList:tktit,
            success (res) {
                var index=res.tapIndex;
                var code=list[index].code;

                that.setData({
                    code:code
                })
                that.getgoodinfo();
            },
            fail (res) {
              console.log(res.errMsg)
            }
          })
    },


    project(){
        wx.switchTab({
          url: '/pages/project/index',
        })
    },

    lists(){
        wx.switchTab({
            url: '/pages/list/index',
          })
    },

    me(){
      wx.switchTab({
        url: '/pages/my/my',
      })
    },
    switchTab(){
      wx.switchTab({
        url: '/pages/index/index',
      })
    },

    getgcode(ret){
        this.setData({
            gcode:ret.detail.value
        })
    },

    wind1show(){
        this.setData({
            wind1:1
        })
    },

    hidewind1(){
        this.setData({
            wind1:0
        })
    },

    setgoodcodebyid(){
        var id=this.data.id;
        var code=this.data.gcode;
        var that=this;

        this.setData({
            code:code
        })
        this.getgoodinfo();
       
    },
    setpaisehide(){
        this.setData({
            paise:0
        })
    },
    setpaiseshow(){
        this.setData({
            paise:1
        })
    },

    getimgs1(){
        let that = this;
        var usrl=api.HOST;
        var id=this.data.id;

        wx.chooseImage({
          count: 1,
          sizeType: ['compressed'], 
          sourceType: ['album', 'camera'], 
          success: function (res) {
            console.log(res)
            let tempFilePaths = res.tempFilePaths;
         
            wx.uploadFile({
              filePath: tempFilePaths[0],
              name: 'file',
              url: usrl+'/api.php?s=portal/index/one',
              success:function(res){
                var data= JSON.parse(res.data);
                that.setData({
                  dtimgs:data.data.img
                });
        
              },
              fail: function (res) {
                console.log('上传失败');
                }
            })
          }
        })
    },
    getimgs2(){
        let that = this;
        var usrl=api.HOST;
        var id=this.data.id;

        wx.chooseImage({
          count: 1,
          sizeType: ['compressed'], 
          sourceType: ['album', 'camera'], 
          success: function (res) {
            console.log(res)
            let tempFilePaths = res.tempFilePaths;
         
            wx.uploadFile({
              filePath: tempFilePaths[0],
              name: 'file',
              url: usrl+'/api.php?s=portal/index/one',
              success:function(res){
                var data= JSON.parse(res.data);
                that.setData({
                  sximgs:data.data.img
                });
        
              },
              fail: function (res) {
                console.log('上传失败');
                }
            })
          }
        })
    },

    previewImage(e) {

        var url=e.currentTarget.dataset.url
        console.log(url);
        wx.previewImage({
            current: url, 
            urls: [url] 
        });

    },


    getimgs(){
        let that = this;
        var usrl=api.HOST;
        var info=this.data.info;
        var user=wx.getStorageSync('user');

        wx.chooseImage({
          count: 1,
          sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
          sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
          success: function (res) {
            console.log(res)
      
            // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
            let tempFilePaths = res.tempFilePaths;
         
            wx.uploadFile({
              filePath: tempFilePaths[0],
              name: 'file',
              url: usrl+'/api.php/portal/index/two?id='+info.id+'&uid='+user.id,
              formData:{},
              success:function(res){
                var data= JSON.parse(res.data);
                info.img=data.data.img;
                    console.log(info);
                    that.setData({
                        info:info
                    });
                 
                    that.getgoodinfo();
        
              },
             
            })
          }
        })
    },

    //转换
    zhuanhuan(){
        let that = this;
        var id=this.data.id;
        var lists=[];
        var list=[];
        api.post({
            url: 'order/getcanzh',
            data: {id:id},
            success: data => {
                
                console.log(data.data)
               
                lists=data.data;

                lists.forEach(function(item){
                    list.push(item.title)
                })

                wx.showActionSheet({
                    itemList:list,
                    success (res) {
                        console.log(res)
                        var i=res.tapIndex;

                        var caseid=lists[i].id;
                        var infoid=that.data.id;

                        console.log(caseid);

                        wx.navigateTo({
                          url: '/pages/about/goods?id='+id+'&caseid='+caseid
                        })

                    }
                  })



            }
        })

        return false;

        

    }
});
